<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>用户列表</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link href="/resources/css/base.css" rel="stylesheet"
	type="text/css" />
<link href="/resources/js/miniui/themes/default/miniui.css"
	rel="stylesheet" type="text/css" />
<script src="/resources/js/jquery-1.6.2.min.js"
	type="text/javascript"></script>
<script src="/resources/js/miniui/miniui.js"
	type="text/javascript"></script>
<style type="text/css">
.errorText {
	color: red;
}
#editWindow {top: 50px !important;left:250px!important}
.mini-checkboxlist-item{    float: left;}
</style>
</head>
<body>
	<h1>用户列表</h1>
	<div style="padding-top: 5px; padding-bottom: 5px; width: 95%;">
		<!--<a class="mini-button"
			img="/resources/js/miniui/themes/icons/add.gif"
			onclick="load4add()">增加</a>  <a class="mini-button"
			img="/resources/js/miniui/themes/icons/edit.gif"
			onclick="editRow()">修改基本信息</a> <a class="mini-button"
			img="/resources/js/miniui/themes/icons/edit.gif"
			onclick="resetPwd()">修改登录密码</a> <a class="mini-button"
			img="/resources/js/miniui/themes/icons/remove.gif"
			onclick="delRow()">删除</a> --> 
	</div>
	<div style="padding-top: 5px; padding-bottom: 5px; width: 95%;">
		<span>手机号：</span><input type="text" id="search_phone" />
		<input type="button" value="查找" onclick="search()" />
	</div>
	<div id="mini-datagrid" class="mini-datagrid"
		style="width: 95%; height: 400px;"
		url="/user/selectUserListByPage" pageSize="20" idField="id"
		allowResize="true">

		<div property="columns">
			<div type="indexcolumn">序号</div>
			<div field="realname">真实姓名</div>
			<div field="idNo">身份证号码</div>
			<div field="phone">手机号</div>
			<div field="school">学校/工作单位</div>
			<div field="address">邮寄地址</div>
			<div field="img">头像</div>
			<!-- <div field="createTime" dateFormat="yyyy-MM-dd HH:mm:ss">注册时间</div> -->
		</div>
	</div>


	<div id="editWindow" class="mini-window" title="用户窗口"
		style="width: 400px;" showModal="true" allowResize="true"
		allowDrag="true">
		<div id="editform" class="form">
			<input class="mini-hidden" name="id" value="0" />
			<table style="width: 100%; text-align: center;">
				<tr>
					<td style="width: 30%; text-align: right;">账号名称：</td>
					<td style="width: 40%; text-align: left;"><input name="phone"
						class="mini-textbox" /></td>
					<td style="width: 30%;" id="phone_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">真实姓名：</td>
					<td style="width: 40%; text-align: left;"><input
						name="realname" class="mini-textbox" /></td>
					<td style="width: 30%;" id="realname_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right;">身份证号码：</td>
					<td style="width: 40%; text-align: left;"><input name="idNo"
						class="mini-textbox" /></td>
					<td style="width: 30%;" id="idNo_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">可用金额：</td>
					<td style="width: 40%; text-align: left;"><input
						name="balance" class="mini-textbox" /></td>
					<td style="width: 30%;" id="balance_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">冻结金额：</td>
					<td style="width: 40%; text-align: left;"><input
						name="freezen" class="mini-textbox" /></td>
					<td style="width: 30%;" id="freezen_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">所属角色：</td>
					<td colspan="2" >
						<div style="margin-left:-100px;display:block;" labelField="true" name="rolesarr" class="mini-checkboxlist" 
							repeatItems="3" repeatLayout="none" url="/role/selectAllRole" id="mini-checkboxlist"
							textField="roleName" valueField="id" >
						</div>
					</td>
				</tr>
				<!-- <tr>
					<td style="width: 30%; text-align: right">是否可用：</td>
					<td style="width: 40%; text-align: left;"><input name="enable"
						class="mini-combobox" valueField="code" textField="data"
						valueFromSelect="true" url="/system/getEnableJson" /></td>
					<td style="width: 30%;" id="enable_error" class="errorText"></td>	
				</tr> -->
				<tr>
					<td
						style="text-align: center; padding-top: 5px; padding-right: 20px;"
						colspan="3"><a class="mini-button"
						img="/resources/js/miniui/themes/icons/save.gif"
						onclick="saveBaseRow()">保存</a> <a class="mini-button"
						img="/resources/js/miniui/themes/icons/cancel.gif"
						onclick="cancelRow()">取消</a></td>
				</tr>
			</table>
		</div>
	</div>

	<div id="resetPwdwindow" class="mini-window" title="修改登录密码"
		style="width: 350px;" showModal="true" allowResize="true"
		allowDrag="true">
		<div id="resetPwdform" class="form">
			<input class="mini-hidden" name="id" value="0" />
			<table style="width: 100%; text-align: center;">
				<tr>
					<td style="width: 30%; text-align: right;">原密码：</td>
					<td style="width: 40%; text-align: left;"><input name="oldPwd"
						class="mini-password" emptyText="请输入原密码" /></td>
					<td style="width: 30%;" id="oldPwd_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">新密码：</td>
					<td style="width: 40%; text-align: left;"><input name="newPwd"
						class="mini-password" emptyText="请输入密码" /></td>
					<td style="width: 30%;" id="newPwd_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">再次输入：</td>
					<td style="width: 40%; text-align: left;"><input
						name="reNewPwd" class="mini-password" emptyText="请输入密码" /></td>
					<td style="width: 30%;" id="reNewPwd_error" class="errorText"></td>
				</tr>
				<tr>
					<td
						style="text-align: center; padding-top: 5px; padding-right: 20px;"
						colspan="6"><a class="mini-button"
						img="/resources/js/miniui/themes/icons/save.gif"
						onclick="saveResetPwdRow()">保存</a> <a class="mini-button"
						img="/resources/js/miniui/themes/icons/cancel.gif"
						onclick="cancelRow()">取消</a></td>
				</tr>
			</table>
		</div>
	</div>

	<div id="resetDealPwdwindow" class="mini-window" title="修改交易密码"
		style="width: 350px;" showModal="true" allowResize="true"
		allowDrag="true">
		<div id="resetDealPwdform" class="form">
			<input class="mini-hidden" name="id" value="0" />
			<table style="width: 100%; text-align: center;">
				<tr>
					<td style="width: 30%; text-align: right;">原密码：</td>
					<td style="width: 40%; text-align: left;"><input name="oldDealPwd"
						class="mini-password" emptyText="请输入原密码" /></td>
					<td style="width: 30%;" id="oldDealPwd_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">新密码：</td>
					<td style="width: 40%; text-align: left;"><input name="newDealPwd"
						class="mini-password" emptyText="请输入密码" /></td>
					<td style="width: 30%;" id="newDealPwd_error" class="errorText"></td>
				</tr>
				<tr>
					<td style="width: 30%; text-align: right">再次输入：</td>
					<td style="width: 40%; text-align: left;"><input
						name="reNewDealPwd" class="mini-password" emptyText="请输入密码" /></td>
					<td style="width: 30%;" id="reNewDealPwd_error" class="errorText"></td>
				</tr>
				<tr>
					<td
						style="text-align: center; padding-top: 5px; padding-right: 20px;"
						colspan="6"><a class="mini-button"
						img="/resources/js/miniui/themes/icons/save.gif"
						onclick="saveResetDealPwdRow()">保存</a> <a class="mini-button"
						img="/resources/js/miniui/themes/icons/cancel.gif"
						onclick="cancelRow()">取消</a></td>
				</tr>
			</table>
		</div>
	</div>
</body>
<script type="text/javascript">
	mini.parse();
	var datagrid = mini.get("mini-datagrid");
	var editWindow = mini.get("editWindow");
	var resetPwdwindow = mini.get("resetPwdwindow");
	var resetPwdform = new mini.Form("resetPwdform");
	var resetDealPwdwindow = mini.get("resetDealPwdwindow");
	var resetDealPwdform = new mini.Form("resetDealPwdform");
	
	var form = new mini.Form("editform");
	var actionType = "insertUser";
	datagrid.load();
	
	function load4add() {
		actionType = "insertUser";
		editWindow.show();
		form.clear();
	}
	
	function editRow() {

		var node = datagrid.getSelected();
		if (node) {
			actionType = "updateUser";
			editWindow.show();
			form.clear();
			
			$.ajax({
				url : "/user/getRoleListById",
				data : {id:node.id},
				type : "post",
				dataType : "json",
				success : function(data) {
					form.setData(node);	
					var checkboxlist = mini.get('mini-checkboxlist');
					checkboxlist.setValue(data.data);
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert(jqXHR.responseText);
				}
			});
			
			
		} else {
			mini.alert("请选择一条!");
		}
	}

	function resetPwd() {
		
		var node = datagrid.getSelected();
		if (node) {
			actionType = "updatePwd";
			resetPwdwindow.show();
			resetPwdform.clear();
			resetPwdform.setData(node);
		} else {
			mini.alert("请选择一条!");
		}
	}
	
	function resetDealPwd() {
	
		var node = datagrid.getSelected();
		if (node) {
			actionType = "updateDealPwd";
			resetDealPwdwindow.show();
			resetDealPwdform.clear();
			resetDealPwdform.setData(node);
		} else {
			mini.alert("请选择一条!");
		}
	}

	function saveBaseRow() {
		var json = form.getData();
		console.log(json);
		if (!validation(json))
			return;
		
		saveRow(json);
	}

	function saveResetPwdRow(){
		
		var data = resetPwdform.getData();
		console.log(data);
		
		if (data.id == 'undefined' || data.id == '') {
			$('#oldPwd_error').html("ID为空");
			return false;
		}
		
		if (data.oldPwd == '') {
			$('#oldPwd_error').empty().html("原密码为空");
			return false;
		} else {
			$('#oldPwd_error').html("");
		}

		if (data.newPwd == '') {
			$('#newPwd_error').html("新密码为空");
			return false;
		} else {
			$('#enable_error').html("");
		}

		if (data.reNewPwd == '' || data.newPwd != data.reNewPwd) {
			$('#reNewPwd_error').html("新密码2次输入有误");
			return false;
		} else {
			$('#reNewPwd_error').html("");
		}
		
		saveRow(data);
	}
	
	function saveResetDealPwdRow(){
		
		var data = resetDealPwdform.getData();
		console.log(data);
		
		if (data.id == 'undefined' || data.id == '') {
			data.id = 0;
			console.log("ID为空");
			return false;
		}
		
		if (data.ldDealPwd == '') {
			$('#oldDealPwd_error').html("原密码为空");
			return false;
		} else {
			$('#oldDealPwd_error').html("");
		}

		if (data.newDealPwd == '') {
			$('#newDealPwd_error').html("新密码为空");
			return false;
		} else {
			$('#newDealPwd_error').html("");
		}

		if (data.reNewDealPwd == '' || data.reNewDealPwd != data.reNewDealPwd) {
			$('#reNewDealPwd_error').html("新密码2次输入有误");
			return false;
		} else {
			$('#reNewDealPwd_error').html("");
		}
		
		saveRow(data);
	}
	
	function saveRow(json) {

		$.ajax({
			url : "/user/" + actionType,
			data : json,
			type : "post",
			dataType : "json",
			success : function(data) {
				if(data.code == 0){
					editWindow.hide();
					datagrid.reload();
					resetPwdwindow.hide();
					resetDealPwdwindow.hide();
				}else{
					mini.alert(data.data);
				}
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert(jqXHR.responseText);
			}
		});

	}

	function cancelRow() {
		datagrid.reload();
		editWindow.hide();
		resetPwdwindow.hide();
		resetDealPwdwindow.hide();
	}

	function delRow() {

		var node = datagrid.getSelected();
		if (node) {
			mini.confirm("确定删除！", "提示", function(retCode) {
				console.log(retCode);
				if (retCode == 'ok') {
					// dosomething 删除
					$.ajax({
						url : "/user/deleteUser",
						data : {
							id : node.id
						},
						type : "post",
						dataType : "json",
						success : function() {
							editWindow.hide();
							datagrid.reload();
						},
						error : function(jqXHR, textStatus, errorThrown) {
							alert(jqXHR.responseText);
						}
					});

				}
			})
		} else {
			mini.alert("请选择一条!");
		}
	}

	function search() {
		datagrid.load({
			phone : $('#search_phone').val()
		});
	}

	function validation(data) {
		if (data.id == 'undefined' || data.id == '') {
			data.id = 0;
		}

		
		if (data.phone == '' || data.phone.length != 11) {
			$('#phone_error').html("phone格式不对");
			return false;
		} else {
			$('#phone_error').html("");
		}
		
		if (data.realname == '') {
			$('#realname_error').html("真实姓名不能为空");
			return false;
		} else {
			$('#realname_error').html("");
		}

		if (data.idNo == '') {
			$('#idNo_error').html("身份证号码不能为空");
			return false;
		} else {
			$('#idNo_error').html("");
		}

		var float_2 = /^\d+(?:\.\d{1,2})?$/;
		if (data.balance == '' || !float_2.test(data.balance)) {
			$('#balance_error').html("可用金额格式错误");
			return false;
		} else {
			$('#balance_error').html("");
		}

		if (data.freezen == '' || !float_2.test(data.freezen)) {
			$('#freezen_error').html("冻结金额格式错误");
			return false;
		} else {
			$('#freezen_error').html("");
		}

		return true;
	}
</script>

</html>
